package com.taoyi.mhouse.fragments;


import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.taoyi.mhouse.R;
import com.taoyi.mhouse.adapter.RecommendHomeAdapter;
import com.taoyi.mhouse.binder.BannerBinder;
import com.taoyi.mhouse.binder.BroadcastingBinder;
import com.taoyi.mhouse.binder.GoodsBinder;
import com.taoyi.mhouse.binder.HeadLineBinder;
import com.taoyi.mhouse.util.UIUtils;

import java.util.Random;

/**
 * Created by tempo on 2015/11/11.
 */
public class RecommendFragment extends LoadingFreshingFragment<String> {

    private RecyclerView rvHome;
    private RecyclerView.LayoutManager layoutManager;
    private RecommendHomeAdapter mAdapter;
    private Handler handler=new Handler();

    private String data;
    @Override
    public void loadData(final LoadingCallback callback) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Random rand = new Random();
                int i= rand.nextInt(3);
                if(i==0) {
                    data = "ok success";
                    callback.setResult(ResultState.Success, data);
                }else if(i==1){
                    callback.setResult(ResultState.Error,null);
                }else{
                    callback.setResult(ResultState.Empty,null);
                }

            }
        },55);
    }

    @Override
    public View getSuccessView(View parent, String data) {
        rvHome = (RecyclerView) View.inflate(UIUtils.getContext(), R.layout.fragment_recommend, null);
        rvHome.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(UIUtils.getContext());
        rvHome.setLayoutManager(layoutManager);
        init();
        return rvHome;
    }

    public void init() {
        mAdapter = new RecommendHomeAdapter();
        mAdapter.putBinder(RecommendHomeAdapter.ViewType.BROADCASTING,new BroadcastingBinder(mAdapter));
        mAdapter.putBinder(RecommendHomeAdapter.ViewType.HEADLINES, new HeadLineBinder(mAdapter));
        mAdapter.putBinder(RecommendHomeAdapter.ViewType.BANNER, new BannerBinder(mAdapter));
        mAdapter.putBinder(RecommendHomeAdapter.ViewType.GOODS, new GoodsBinder(mAdapter));
        rvHome.setAdapter(mAdapter);
    }


    @Override
    protected void clearData() {
        data=null;
    }
}
